package cn.weforward.task.weforward;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.weforward.common.ResultPage;
import cn.weforward.common.util.StringUtil;
import cn.weforward.common.util.TransResultPage;
import cn.weforward.data.log.BusinessLog;
import cn.weforward.framework.ApiException;
import cn.weforward.framework.WeforwardMethod;
import cn.weforward.framework.WeforwardMethods;
import cn.weforward.framework.doc.DocMethods;
import cn.weforward.framework.exception.ForwardException;
import cn.weforward.framework.util.ValidateUtil;
import cn.weforward.protocol.client.util.IdBean;
import cn.weforward.protocol.doc.annotation.DocAttribute;
import cn.weforward.protocol.doc.annotation.DocMethod;
import cn.weforward.protocol.doc.annotation.DocParameter;
import cn.weforward.task.Bug;
import cn.weforward.task.Task;
import cn.weforward.task.TaskService;
import cn.weforward.task.impl.SimpleBug;
import cn.weforward.task.weforward.view.BugParam;
import cn.weforward.task.weforward.view.BugView;
import cn.weforward.task.weforward.view.LogView;
import cn.weforward.task.weforward.view.LogsParams;
import cn.weforward.task.weforward.view.UpdateBugParam;
/**  
 * bug接口
 *
 */
@DocMethods(index = 200)
@WeforwardMethods
public class BugMethods {
	@Resource
	TaskService m_Service;
	
	final static Logger _Logger = LoggerFactory.getLogger(BugMethods.class);
	
	@WeforwardMethod
	@DocMethod(description = "创建一个bug",index = 1)
	public BugView create(BugParam params) throws ApiException {
		ValidateUtil.isEmpty(params.getContent(), "bug内容不能为空");
		ValidateUtil.isEmpty(params.getTaskId(), "任务id不能为空");
		ValidateUtil.isEmpty(params.getHandler(), "bug处理人不能为空");
		ValidateUtil.isEmpty(params.getSeverity(), "bug严重性不能为空");
		ValidateUtil.isEmpty(params.getVersionAndPlatform(), "bug版本与平台不能为空");
		//先从service根据id获取一个task
		Task task = m_Service.getTask(params.getTaskId());
		
		ValidateUtil.isEmpty(task, "没有该任务id");
		//通过调用这个bo下的方法去创建一个bug
		Bug bug = task.createBug(params.getContent(), params.getHandler(), params.getSeverity(), params.getVersionAndPlatform());
		
		return BugView.valueOf(bug);
	
	}
	
	@WeforwardMethod
	@DocParameter({@DocAttribute(name = "id", type = String.class, necessary = true, description = "任务id"),
					@DocAttribute(name = "page", type = Integer.class, necessary = false, description = "第几页"),
					@DocAttribute(name = "pageSize", type = Integer.class, necessary = false, description = "一页数据大小")})
	@DocMethod(description = "根据任务id查询bugs",index = 2)
	public ResultPage<BugView> getBugsByTaskId(IdBean params) throws ApiException{
		String taskId = params.getId();
		ValidateUtil.isEmpty(taskId, "任务id不能为空");
		Task task = m_Service.getTask(taskId);
		ValidateUtil.isEmpty(task, "该任务id不存在");
		ResultPage<SimpleBug> rp = task.getBugs(taskId);
		
		return new TransResultPage<BugView, SimpleBug>(rp) {
			@Override
			protected BugView trans(SimpleBug src) {
				// TODO Auto-generated method stub
				return BugView.valueOf(src);
			}
			
		};
	}
	
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", type = String.class, necessary = true, description = "bug的id"))
	@DocMethod(description = "获取bug",index = 3)
	public BugView get(IdBean params) throws ApiException{
		Bug bug = m_Service.getBug(params.getId());
		ValidateUtil.isEmpty(bug, "该Bug的id不存在");
		return BugView.valueOf(bug);
	}
	
	@WeforwardMethod
	@DocMethod(description = "修改一个bug",index = 3)
	public BugView update(UpdateBugParam params) throws ApiException {
		ValidateUtil.isEmpty(params.getId(),"id不能为空" );
		Bug bug = m_Service.getBug(params.getId());
		ValidateUtil.isEmpty(bug,"该bug的id不存在" );
		
		String content = params.getContent();
		if(!StringUtil.isEmpty(content)) {
			bug.setContent(content);
		}
		
		String handler = params.getHandler();
		if(!StringUtil.isEmpty(handler)) {
			bug.setHandler(handler);
		}
		
		String severity = params.getSeverity();
		if(!StringUtil.isEmpty(severity)) {
			bug.setSeverity(severity);
		}
		
		String tester = params.getTester();
		if(!StringUtil.isEmpty(tester)) {
			bug.setTester(tester);
		}
		String versionAndPlatform = params.getVersionAndPlatform();
		if(!StringUtil.isEmpty(versionAndPlatform)) {
			bug.setVersionAndPlatform(versionAndPlatform);
		}
		
		return BugView.valueOf(bug);
	}
	
	
	@WeforwardMethod
	@DocParameter({@DocAttribute(name = "id", type = Integer.class, necessary = true, description = "BugId"),
		@DocAttribute(name = "page", type = Integer.class, necessary = false, description = "第几页"),
		@DocAttribute(name = "pageSize", type = Integer.class, necessary = false, description = "一页数据大小")})
	@DocMethod(description = "获取bug日志", index = 4)
	public ResultPage<LogView> logs(LogsParams params) throws ApiException {
		String id = params.getId();
		ValidateUtil.isEmpty(id, "id不能为空");
		Bug bug = m_Service.getBug(id);
		ValidateUtil.isNull(bug, "该任务下没有bug");
		ForwardException.forwardToIfNeed(bug);
		return new TransResultPage<LogView, BusinessLog>(bug.getLogs()) {
			@Override
			protected LogView trans(BusinessLog src) {
				return LogView.valueOf(src);
			}
		};
	}
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", necessary = true,type = String.class, description = "BugId"))
	@DocMethod(description = "状态扭转为待修正",index = 5)
	public BugView turnGonnaAmend(IdBean params) throws ApiException {
		Bug bug = m_Service.getBug(params.getId());
		
		ValidateUtil.isEmpty(bug, "该bug的Id不存在");
		
		bug.gonnaAmend();
		
		return BugView.valueOf(bug);
	}
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", necessary = true,type = String.class, description = "BugId"))
	@DocMethod(description = "状态扭转为待复测",index = 6)
	public BugView turnGonnaRetest(IdBean params) throws ApiException {
		Bug bug = m_Service.getBug(params.getId());
		
		ValidateUtil.isEmpty(bug, "该bug的Id不存在");
		
		bug.gonnaRetest();
		
		return BugView.valueOf(bug);
	}
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", necessary = true,type = String.class, description = "BugId"))
	@DocMethod(description = "状态扭转为建议不作修改",index = 7)
	public BugView turnAdviceNoUpdate(IdBean params) throws ApiException {
		Bug bug = m_Service.getBug(params.getId());
		
		ValidateUtil.isEmpty(bug, "该bug的Id不存在");
		
		bug.adviceNoUpdate();
		
		return BugView.valueOf(bug);
	}
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", necessary = true,type = String.class, description = "BugId"))
	@DocMethod(description = "状态扭转为申请无法解决",index = 8)
	public BugView turnApplyCantSolve(IdBean params) throws ApiException {
		Bug bug = m_Service.getBug(params.getId());
		
		ValidateUtil.isEmpty(bug, "该bug的Id不存在");
		
		bug.applyCantSolve();
		
		return BugView.valueOf(bug);
	}
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", necessary = true,type = String.class, description = "BugId"))
	@DocMethod(description = "状态扭转为已解决",index = 9)
	public BugView turnSolve(IdBean params) throws ApiException {
		Bug bug = m_Service.getBug(params.getId());
		
		ValidateUtil.isEmpty(bug, "该bug的Id不存在");
		
		bug.solve();
		
		return BugView.valueOf(bug);
	}
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", necessary = true,type = String.class, description = "BugId"))
	@DocMethod(description = "状态扭转为不作修改",index = 10)
	public BugView turnNoUpdate(IdBean params) throws ApiException {
		Bug bug = m_Service.getBug(params.getId());
		
		ValidateUtil.isEmpty(bug, "该bug的Id不存在");
		
		bug.noUpdate();
		
		return BugView.valueOf(bug);
	}
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", necessary = true,type = String.class, description = "BugId"))
	@DocMethod(description = "状态扭转为无法解决",index = 11)
	public BugView turnCantSolve(IdBean params) throws ApiException {
		Bug bug = m_Service.getBug(params.getId());
		
		ValidateUtil.isEmpty(bug, "该bug的Id不存在");
		
		bug.cantSolve();
		
		return BugView.valueOf(bug);
	}
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", necessary = true,type = String.class, description = "BugId"))
	@DocMethod(description = "状态扭转为重新打开",index = 12)
	public BugView turnReopen(IdBean params) throws ApiException {
		Bug bug = m_Service.getBug(params.getId());
		
		ValidateUtil.isEmpty(bug, "该bug的Id不存在");
		
		bug.reOpen();
		
		return BugView.valueOf(bug);
	}


	
}
